import sqlite3
import time

def main(self, sender, channel, flags, message, DB, DEBUG):

    #store and retrieve messages for users

    con=sqlite3.connect(DB)
    cur=con.cursor()

     #store messages in database for future retrieval (when a user comes online)
    def tell_user_store(sender,user,message):
        date = time.ctime()
        cur.execute("INSERT INTO tell (user_from, user_to, date_time, message_text) VALUES (?, ?, ?, ?)", (sender.nickname, user, date, message))
        
    #when a user sends a message to a channel, find any stored messages and queue sending them to that user, then delete stored messages from db
    def tell_user_retrieve(sender):
        messages_retrieved = []
        messages = cur.execute("SELECT user_from, user_to, date_time, message_text from tell where user_to = ?", (sender.nickname,)).fetchall()

        #deletes messages from database once sent since we don't want to resend infinitely many times
        cur.execute("DELETE FROM tell where user_to = ?", (sender.nickname,))
        for message in messages:
            messages_retrieved.append(message)
        return messages_retrieved

    #parses message looking for tell keyword at the beginning
    if message.startswith("tell "):
        message_tell = message.replace('tell ', '')
        user = message_tell.split()
        user = user[0]
        message_tell = message.replace(user, '')
        tell_user_store(sender,user,message)

    #actually sends the user stored messages
    messages_retrieved = tell_user_retrieve(sender)
    for message_tell in messages_retrieved:
        self.send_private_message(sender, "%s " %message_tell[2] + ":: %s" %message_tell[0] + " left you a message :: %s" %message_tell[3])

    cur.close()
    con.commit()
